package com.zrwl.poscloud.xo.vo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zrwl.poscloud.commons.vo.BaseVO;
import lombok.Data;

/**
 * 操作员日志 VO类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-11-23
 */
@Data
public class AdminLogVO extends BaseVO<AdminLogVO> {
    private static final long serialVersionUID = 1L;
    private String configUid;
    private int logLevel;
    private int businessType;
    private int businessName;
    private int operationType;
    private String adminUid;
    private String name;
    private String ip;
    private String summary;
    private double amt1;
    private double amt2;
    private double amt3;
    private double amt4;
    private double amt5;
    private double amt6;
    private int count1;
    private int count2;
    private int count3;
    private int count4;
    private String keyword1;
    private String keyword2;
    private String keyword3;
    private String keyword4;
    private String keyword5;
    private String oldData;
    private String newData;
    private String columnsConfig;
    private String dataConfig;
    private int sourceType;
    private String sourceUid;
    private String sourceName;

    public enum LOGLEVEL {
        INFO("info", 1),
        REPORT("report", 2);

        private String typeName;
        public int value;

        LOGLEVEL(String typeName, int value) {
            this.typeName = typeName;
            this.value = value;
        }

        /***
         * 根据名称查对象
         * @param typeName
         * @return
         */
        public static LOGLEVEL fromTypeName(String typeName) {
            for (LOGLEVEL type : LOGLEVEL.values()) {
                if (type.typeName.equals(typeName)) {
                    return type;
                }
            }
            return null;
        }
    }

    public enum OPERATIONTYPE {
        CREATE("create", 1),
        QUERYFOR("queryFor", 2),
        UPDATE("update", 3),
        DELETE("delete", 4),
        QUERYFOREXPORT("queryForExport", 5),
        BATCH_CREATE("batch_create", 6),
        BATCH_UPDATE("batch_update", 7),
        BATCH_DELETE("batch_delete", 8),
        EQUIPMENTCONFIG_CREATE("equipmentConfig_create", 9),
        EQUIPMENTCONFIG_UPDATE("equipmentConfig_update", 10),
        EQUIPMENTCONFIG_DELETE("equipmentConfig_delete", 11),
        ABNORMALTRADE_VALID("abnormalTrade_valid", 12),
        ABNORMALTRADE_INVALID("abnormalTrade_invalid", 13),
        RECHARGETRADE_VALID("rechargeTrade_valid", 14),
        RECHARGETRADE_INVALID("rechargeTrade_invalid", 15);

        private String typeName;
        public int value;

        OPERATIONTYPE(String typeName, int value) {
            this.typeName = typeName;
            this.value = value;
        }
    }

    public AdminLogVO(LOGLEVEL logLevel, int businessType, OPERATIONTYPE operationType, String summary, Object oldData, Object newData, Object... columns) {
        this.logLevel = logLevel.value;
        this.businessType = businessType;
        this.operationType = operationType.value;
        if (summary != null && summary.length() > 100) summary = summary.substring(0, 100);
        this.summary = summary;
        if (oldData != null)
            this.oldData = JSON.toJSONStringWithDateFormat(oldData, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat);
        if (newData != null)
            this.newData = JSON.toJSONStringWithDateFormat(newData, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat);
        int amtIndex = 1;
        int countIndex = 1;
        int keywordIndex = 1;
        for (int i = 0; i < columns.length; i++) {
            Object column = columns[i];
            if (column == null) continue;
            switch (column.getClass().getSimpleName()) {
                case "Double":
                    switch (amtIndex++) {
                        case 1:
                            this.amt1 = (double) column;
                            break;
                        case 2:
                            this.amt2 = (double) column;
                            break;
                        case 3:
                            this.amt3 = (double) column;
                            break;
                        case 4:
                            this.amt4 = (double) column;
                            break;
                        case 5:
                            this.amt5 = (double) column;
                            break;
                        case 6:
                            this.amt6 = (double) column;
                            break;
                    }
                    break;
                case "Integer":
                    switch (countIndex++) {
                        case 1:
                            this.count1 = (int) column;
                            break;
                        case 2:
                            this.count2 = (int) column;
                            break;
                        case 3:
                            this.count3 = (int) column;
                            break;
                        case 4:
                            this.count4 = (int) column;
                            break;
                    }
                    break;
                case "String":
                    String columnStr = (String) column;
                    switch (keywordIndex++) {
                        case 1:
                            if (columnStr.length() > 20) columnStr = columnStr.substring(0, 20);
                            this.keyword1 = columnStr;
                            break;
                        case 2:
                            if (columnStr.length() > 20) columnStr = columnStr.substring(0, 20);
                            this.keyword2 = columnStr;
                            break;
                        case 3:
                            if (columnStr.length() > 20) columnStr = columnStr.substring(0, 20);
                            this.keyword3 = columnStr;
                            break;
                        case 4:
                            if (columnStr.length() > 50) columnStr = columnStr.substring(0, 50);
                            this.keyword4 = columnStr;
                            break;
                        case 5:
                            if (columnStr.length() > 50) columnStr = columnStr.substring(0, 50);
                            this.keyword5 = columnStr;
                            break;
                    }
                    break;
            }
        }
    }

    public AdminLogVO() {
    }
}
